<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 15:12
 */
class Api_Index extends PhalApi_Api{
    public function getRules(){
        return array(
            'getGift' => array(   //用户当天抽奖判断情况
              'phone' => array('name'=>'phone','require'=>true),
            ),
            'makeOrder'=>array(
              'phone'=>array('name'=>'phone','require'=>true),
              'shopId'=>array('name'=>'shopId','require'=>true),
            ),
            'getAdr'=>array(
              'prizeId'=>array('name'=>'prizeId','require'=>true),
            ),
            'InsertAdr'=>array(
              'userid'=>array('name'=>'userid','require'=>true),
              'recivephone'=>array('name'=>'recivephone','require'=>true),
              'recivename'=>array('name'=>'recivename','require'=>true),
              'reciveAdr'=>array('name'=>'reciveAdr','require'=>true),
              'prizeId'=>array('name'=>'prizeId','require'=>true),
            ),
            'getCoupon'=>array(
              'phone'=>array('name'=>'phone','require'=>true),
            ),
        );
    }

  /**
   * 下单后，插入用户和店铺对应表
   */
  public function makeOrder(){
    $Domain_ShopUserBuy=new Domain_ShopUserBuy();
    return $Domain_ShopUserBuy->makeOrder($this->phone,$this->shopId);
  }

  /**
   * 用户当天抽奖
   */
    public function getGift(){
        $Domain_Gift = new Domain_Index();
        return $Domain_Gift -> getGift($this->phone);
    }

  /**
   * 收货地址
   */
  public function InsertAdr(){
    $Domain_PrizeRecordAdr=new Domain_PrizeRecordAdr();
     $Domain_PrizeRecordAdr->InsertAdr($this->userid,$this->prizeId,$this->recivephone
    ,$this->recivename,$this->reciveAdr);
    return true;
  }

  /**
   * 获取领奖地址
   */
  public function getAdr(){
    $Domain_Prize=new Domain_Prize();
    return $Domain_Prize->getAdr($this->prizeId);
  }

  public function getCoupon(){
    $Domain_SharePrizeRecord=new Domain_SharePrizeRecord();
    //查看今天是否已领取
    $couponCount=$Domain_SharePrizeRecord->checkIsGetCoupon($this->phone);
    if($couponCount>0)return -1;
    //查看奖池是否有剩余
    $Domain_Prize=new Domain_Prize();
    $balance=$Domain_Prize->getBalance(6);//奖品池每日可领取量
    $prizeRecord=$Domain_SharePrizeRecord->getPrizeRecord(6);//奖品池已领取量
    if($prizeRecord>=$balance)
      return -2;//无剩余
    //中奖
     $Domain_SharePrizeRecord->getCoupon($this->phone,6);
    return 1;
  }
}